import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Thread_CachethreadPool {
	// 拒绝task的处理策略
	RejectedExecutionHandler rejectedHander = new RejectedExecutionHandler() {
		@Override
		public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
			if (LcRunable.class == r.getClass())
				System.out.println(LcRunable.class.cast(r).getName() + "==========rejectedExecution");
		}
	};

	ThreadPoolExecutor service = new ThreadPoolExecutor(5, 200, 30L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1000),
			Executors.defaultThreadFactory(), rejectedHander);

	public void exec(LcRunable runable) {
		// service.execute(runable);
		Future<?> fu = service.submit(runable);
	}

	static class LcRunable implements Runnable {
		private String name;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public LcRunable(String name) {
			super();
			this.name = name;
		}

		@Override
		public void run() {
			// if (name.endsWith("99")) {
			System.out.println(name);

			// }
		}

	}

	public static void main(String[] args) throws ExecutionException, InterruptedException {
		final Thread_CachethreadPool pool = new Thread_CachethreadPool();
		for (int i = 0; i < 30; i++) {
			final int xx = i;
			new Thread(new Runnable() {
				@Override
				public void run() {
					for (int j = 0; j < 100; j++) {
						pool.exec(new LcRunable("thread" + xx + "_" + j));
					}
				}
			}).start();
		}

	}
}
